package com.youlu.campus.admin.course.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.course.dto.ActivityCourseBindDTO;
import com.youlu.campus.admin.course.dto.ActivityCourseSaveDTO;
import com.youlu.campus.admin.course.dto.ActivityCourseSearchDTO;
import com.youlu.campus.admin.course.service.ActivityCourseService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.entity.course.ActivityCourse;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 活动课程
 *
 * @author : [dengbo]
 * @className : ActivityCourseController
 * @description : [活动课程]
 * @createTime : [2022/5/16 18:08]
 */
@Slf4j
@RestController
public class ActivityCourseController {
    @Resource
    private ActivityCourseService ActivityCourseService;

    @Autowired
    private RoleService roleService;

    /**
     * 课程列表
     * @param request 课程列表查询请求类
     * @return
     */
    @PostMapping("/activity-course/list")
    public QResult list(@Validated @RequestBody ActivityCourseSearchDTO request) {
        log.info("ActivityCourseController,list,request param:{}", JSON.toJSONString(request));
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        if(CollectionUtils.isNotEmpty(roleList)){
            //如果角色等于期末帮
            long count = roleList.stream().filter(systemRole -> "期末帮".contains(systemRole.getName())).count();
            if(count>0){
                request.setQiMoBangRole(Boolean.TRUE);
            }
        }
        request.setUserId(userId);
        Page<ActivityCourse> data = ActivityCourseService.list(request);
        log.info("ActivityCourseController,list,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 课程保存
     * @param request 课程保存修改请求类
     * @return
     */
    @PostMapping("/activity-course/save")
    public QResult save(@Validated @RequestBody ActivityCourseSaveDTO request) {
        log.info("ActivityCourseController,save,request param:{}", JSON.toJSONString(request));
        String userId = AuthUtils.getUserId();
        request.setCreatorId(userId);
        boolean re = ActivityCourseService.save(request);
        log.info("ActivityCourseController,save,response:{}", re);
        return QResult.success(re);
    }

    /**
     * 活动课程绑定
     * @param request 活动课程绑定请求类
     * @return
     */
    @PostMapping("/activity-course/bind")
    public QResult bind(@Validated @RequestBody ActivityCourseBindDTO request) {
        log.info("ActivityCourseController,bind,request param:{}", JSON.toJSONString(request));
        boolean re = ActivityCourseService.bind(request);
        log.info("ActivityCourseController,bind,response:{}", re);
        return QResult.success(re);
    }
}
